#include "StdAfx.h"
#include ".\configparse.h"

#define	NONE	-1
#define MAX_SIZE 2048


class CReadProfile
{
public:
	CReadProfile(const char *fname) 
		: m_f(NULL)
		{ m_strFname = fname; }
	CReadProfile() 
		{ }
	~CReadProfile()
		{ }

	BOOL open() {
		m_f = fopen(m_strFname, "r+");
		if (!m_f) 
			return false;
		return true;
	}

	BOOL readLine(CString &strline) {
		int i = 0;
		while (!feof(m_f)) {
			if (i < (MAX_SIZE-1) ) {
				if(1 == fread((void *)&m_line[i], sizeof(char), 1, m_f)) {
					if (m_line[i] == '\r' || m_line[i] == '\n' || m_line[i] == '\0') {
						m_line[i] = 0;
						strline = m_line;
						return	TRUE;
					}
				}
			}	
			else {
				m_line[MAX_SIZE-1] = '\0';
				strline = m_line;
				return TRUE;
			}

			i++;
		}
		
		return FALSE;
	}

	void parse_linea(IN CString  strLinea, OUT CString &strSec,OUT CString &strVal) {
#ifdef _DEBUG
		printf("DEBUG: codec profile linea string: %s\n", strLinea);
#endif			
		int pos = strLinea.Find('=');
		if (pos == -1) 
			return;

		strSec = strLinea.Mid(0, pos);
		strVal = strLinea.Mid(pos+1, strLinea.GetLength());
#ifdef _DEBUG
		printf("DEBUG SEC:%s VAL:%s\n", strSec, strVal);
#endif
	}
	
	void close() 
	{
		if (m_f) {
			fclose(m_f);
			m_f = NULL;
		}
	}

protected:
	char m_line[MAX_SIZE];
	CString m_strFname;
	FILE *m_f;
}; //class CReadProfile




//static CAudioFormatDlg *g_CAduioFormatDlg = NULL;

CConfigParse::CConfigParse(void)
{
	m_pCmb_profile = NULL;
	m_cur_profile = NONE;
}

CConfigParse::CConfigParse(IN int profile)
{
	m_pCmb_profile = NULL;
	m_cur_profile = profile;
}


CConfigParse::CConfigParse(IN CWnd *pWnd)
{
	m_pCmb_profile = (CComboBox *)pWnd;

	CString strProfile;
	CString strDir;
	char temp_dir[MAX_PATH], *wptr; 

	GetModuleFileNameA(NULL, temp_dir, MAX_PATH);	
	wptr = strrchr(temp_dir, '\\');
	*wptr = '\0'; 


#ifdef _UNICODE
	WCHAR szProName[256], strValue[256];
#else
	char szProName[256], strValue[256];
#endif
	strProfile = temp_dir;
	int index = m_pCmb_profile->GetCurSel();
	m_pCmb_profile->GetLBText(index, szProName);
	strProfile += _T("\\profile\\");
	strProfile += szProName;
	strProfile += _T(".ini");

	m_profile_name = strProfile;

#ifdef _DEBUG
	#ifdef _UNICODE
		GetPrivateProfileStringW(_T("Settings"), _T("VideoEnc"), NULL, strValue, 256, strProfile);
	#else
		GetPrivateProfileStringA("Settings", "VideoEnc", NULL, strValue, 256, strProfile);
	#endif
#endif //_DEBUG


	//

}


void CConfigParse::set_Combox_profile(IN CWnd *pWnd)
{
	m_pCmb_profile = (CComboBox *)pWnd;

	CString strProfile;
	CString strDir;
	char temp_dir[MAX_PATH], *wptr; 

	GetModuleFileNameA(NULL, temp_dir, MAX_PATH);	
	wptr = strrchr(temp_dir, '\\');
	*wptr = '\0'; 


#ifdef _UNICODE
	WCHAR szProName[256], strValue[256];
#else
	char szProName[256], strValue[256];
#endif
	strProfile = temp_dir;
	int index = m_pCmb_profile->GetCurSel();
	m_pCmb_profile->GetLBText(index, szProName);
	strProfile += _T("\\profile\\");
	strProfile += szProName;
	strProfile += _T(".ini");

	m_profile_name = strProfile;

#ifdef _DEBUG
#ifdef _UNICODE
	GetPrivateProfileStringW(_T("Settings"), _T("VideoEnc"), NULL, strValue, 256, strProfile);
#else
	GetPrivateProfileStringA("Settings", "VideoEnc", NULL, strValue, 256, strProfile);
#endif
#endif //_DEBUG

	//for testing
	CString strlinea, strSec, strVal;
	CReadProfile profile(strProfile);
	BOOL ret = profile.open();
	while(ret) {
		ret = profile.readLine(strlinea);
		profile.parse_linea(strlinea, strSec, strVal);
		parse_vcodec(strSec, strVal);
		parse_acodec(strSec, strVal);
	}
	profile.close();
	//end
}

CConfigParse::~CConfigParse(void)
{
}

CString CConfigParse::get_sectiong_values(const char *sec_name)
{
	char szValue[MAX_PATH];

#ifdef _UNICODE
	GetPrivateProfileStringW(_T("Settings"), _T("VideoEnc"), NULL, strValue, 256, strProfile);
#else
	GetPrivateProfileStringA("Settings", sec_name, NULL, szValue, 256, m_profile_name);
#endif

	return szValue;
}


int	CConfigParse::get_cur_profile()
{

	return NONE;
}


CString CConfigParse::parse_vcodec()
{
	CString str_menc;
	CString values;
	values = get_sectiong_values(CONFIG_VideoEnc);

	if (!values.CompareNoCase("x264")) {
		//str_menc = "-ovc x264 -ffourcc H264";		
	}


	return values;
}


void CConfigParse::parse_vcodec(CString &strSec, CString &strVal)
{
	/************************************************************************/
	/* 
		standard video setting here
	*/
	/************************************************************************/
	if (!strSec.CompareNoCase(CONFIG_VideoEnc)) {
		strcpy(m_fmtVideoCodec.fmt_v_codec, strVal);
	}
	else if (!strSec.CompareNoCase(CONIFG_VideoBR)) {
		strcpy(m_fmtVideoCodec.fmt_v_bitrate, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_VideoSize)) {
		strcpy(m_fmtVideoCodec.fmt_v_vsize, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_Videofps)) {
		strcpy(m_fmtVideoCodec.fmt_v_fr, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_ScaleUpTo)) {
		strcpy(m_fmtVideoCodec.fmt_v_uptoscale, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_Aspect)) {
		strcpy(m_fmtVideoCodec.fmt_v_aspect, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_VideoMthread)) {
		strcpy(m_fmtVideoCodec.fmt_v_thread, strVal);
	}
	/************************************************************************/
	/* 
		extra video filter setting here
	*/
	/************************************************************************/
	else if (!strSec.CompareNoCase(CONFIG_Xcmd)) { //opts
		strcpy(m_fmtVideoCodec.fmt_v_Xcmd, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_Xvc)) { //video codec's setting
		strcpy(m_fmtVideoCodec.fmt_v_Xvc, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_Xvfpre)) { //video pre filter
	}
	else if (!strSec.CompareNoCase(CONFIG_Xvf)) { //video filter
		strcpy(m_fmtVideoCodec.fmt_v_Xvf, strVal);	
	}
	else if (!strSec.CompareNoCase(CONFIG_crf)) { //video quality
		m_fmtVideoCodec.fmt_v_quality = atoi(strVal);
	}
}

void CConfigParse::parse_acodec(CString &strSec, CString &strVal)
{
	/************************************************************************/
	/* 
		common settting
	*/
	/************************************************************************/
	if (!strSec.CompareNoCase(CONFIG_AudioEnc)) {
		strcpy(m_fmtAudioCodec.fmt_acodec_name, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_AudioBR)) {
		m_fmtAudioCodec.s_aac.fmt_bitrate = atoi(strVal);
		m_fmtAudioCodec.s_ac3.fmt_bitrate = atoi(strVal);
		m_fmtAudioCodec.s_mp2.fmt_bitrate = atoi(strVal);
		m_fmtAudioCodec.s_mp3.fmt_bitrate = atoi(strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_AudioHz)) {
		m_fmtAudioCodec.s_aac.fmt_sample = atoi(strVal);
		m_fmtAudioCodec.s_ac3.fmt_sample = atoi(strVal);
		m_fmtAudioCodec.s_mp2.fmt_sample = atoi(strVal);
		m_fmtAudioCodec.s_mp3.fmt_sample = atoi(strVal);
	}
	/************************************************************************/
	/* 
		extra setting
	*/
	/************************************************************************/
	else if (!strSec.CompareNoCase(CONFIG_AACMpeg)) {
		m_fmtAudioCodec.s_aac.fmt_mpegtype = atoi(strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_AACObject)) {
		m_fmtAudioCodec.s_aac.fmt_objectype = atoi(strVal);

	}
	else if (!strSec.CompareNoCase(CONFIG_Xac)) { //audio codec filter

	}
	else if (!strSec.CompareNoCase(CONFIG_Xaf)) { //audio pre filter
		strcpy(m_fmtAudioCodec.fmt_acodec_Xaf, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_Xss)) { //start time

	}
	else if (!strSec.CompareNoCase(CONFIG_Xendpos)) { //end time

	}
	else if (!strSec.CompareNoCase(CONFIG_Xof)) { // out file container
		m_strProfileXof = strVal;
	}
	/************************************************************************/
	/* 
		mp3
	*/
	/************************************************************************/
	else if (!strSec.CompareNoCase(CONFIG_MP3Mode)) {
		strcpy(m_fmtAudioCodec.s_mp3.fmt_mp3mode, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_MP3Encode)) {
		strcpy(m_fmtAudioCodec.s_mp3.fmt_mp3encode,strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_MP3AQ)) {
		strcpy(m_fmtAudioCodec.s_mp3.fmt_mp3AQ, strVal);
	}
	else if (!strSec.CompareNoCase(CONFIG_MP3Vol)) {
		strcpy(m_fmtAudioCodec.s_mp3.fmt_mp3volume, strVal);
	}
	/************************************************************************/
	/* 
		
	*/
	/************************************************************************/
}


CString CConfigParse::parse_acodec()
{
	CString values;
	values = get_sectiong_values(CONFIG_AudioEnc);
	
		

	return values;
}

/************************************************************************/
/* 
	\samples
	\bitrates
	\mpeg type
	\object type

	extras: \streams no
			\filters
*/
/************************************************************************/
BOOL CConfigParse::parse_aac(CString &strVal)
{
	if(!strVal.CompareNoCase("aac"))
		return TRUE;

	return FALSE;
}

/************************************************************************/
/* 
	\samples
	\bitrates
	\encode: cbr:abr:vbr
	\mode: stereo ...
	\volume
	\AQ

	extras: \stream no
			\filters
*/
/************************************************************************/
BOOL CConfigParse::parse_mp3(CString &strVal)
{
	if(!strVal.CompareNoCase("mp3"))
		return TRUE;
	
	return FALSE;
}

/************************************************************************/
/* 
	\samples
	\bitrate
	extras: \stream no
			\filter
*/
/************************************************************************/
BOOL CConfigParse::parse_mp2(CString &strVal)
{
	if(!strVal.CompareNoCase("mp2"))
		return TRUE;

	return FALSE;
}

BOOL CConfigParse::parse_ac3(CString &strVal)
{
	if(!strVal.CompareNoCase("ac3"))
		return TRUE;

	return FALSE;
}

BOOL CConfigParse::parse_acopy(CString &strVal)
{
	if(!strVal.CompareNoCase("mp3"))
		return TRUE;

	return FALSE;
}

BOOL CConfigParse::parse_NoAudio(CString &strVal)
{
	if(!strVal.CompareNoCase("mp3"))
		return TRUE;
	
	return FALSE;
}

/************************************************************************/
/* 
	\bps, 
	\aspect, 
	\framerate, 
	\scale, 
	\pixel ratio, 
	\threads
*/
/************************************************************************/
CString CConfigParse::parse_x264()
{
	CString str_menc;
	CString str_br;
	CString str_aspect, str_fr, str_scaleUpto, str_pixelRatio, str_thread; 

	str_menc = "-ovc x264 -ffourcc H264 -x264encopts ";
	str_br = get_sectiong_values(CONIFG_VideoBR);
	str_aspect = get_sectiong_values(CONFIG_VideoSize);
	str_fr = get_sectiong_values(CONFIG_Videofps);
	str_scaleUpto = get_sectiong_values(CONFIG_ScaleUpTo);
	str_pixelRatio = get_sectiong_values(CONFIG_Aspect);
//	str_thread = get_sectiong_values();
	str_br = get_sectiong_values(CONIFG_VideoBR);
	
	return "";

}

/************************************************************************/
/*
	\bps, 
	\aspect, 
	\framerate, 
	\scale up to, 
	\display aspect ratio, 
	\threads 
*/
/************************************************************************/
CString CConfigParse::parse_Divx()
{
	return "";
}

/************************************************************************/
/*
	\bps
	\aspect
	\fps
	\scale up to
	\display aspect ratio
	\threads
*/
/************************************************************************/
CString CConfigParse::parse_Xvid()
{
	return "";
}

/************************************************************************/
/*
	\bps
	\aspect
	\fps
	\scale up to
	\display aspect ratio
	\threads
*/
/************************************************************************/
CString CConfigParse::parse_mpeg1()
{
	return "";
}

/************************************************************************/
/*
	\bps
	\aspect
	\fps
	\scale up to
	\display aspect ratio
	\threads
*/
/************************************************************************/
CString CConfigParse::parse_mpeg2()
{
	return "";
}

CString CConfigParse::parse_vcopy()
{
	return "";
}

CString CConfigParse::parse_NoVideo()
{
	return "";
}

/************************************************************************/
/* 
	here setting A/V codec information
*/
/************************************************************************/
void CConfigParse::Notify_setting_param(const char *szType, const char *strVal, const int intVal)
{
	CString strInt, strType, str_values;

	strType = szType;
	str_values = strVal;

	if (!strVal) {
		strInt.Format("%d", intVal);
		parse_vcodec(strType, strInt);
		parse_acodec(strType, strInt);
	}
	else if (intVal == -1) {
		parse_vcodec(strType, str_values);
		parse_acodec(strType, str_values);
	}
	return;
}


void CConfigParse::set_OutputFormat(const char *szOutFormat)
{
	m_strOutFormat = szOutFormat;
}